package com.mindskip.xzs.repository;

import com.mindskip.xzs.domain.UserEventLog;
import com.mindskip.xzs.domain.other.KeyValue;
import com.mindskip.xzs.repository.base.MyMapper;
import com.mindskip.xzs.viewmodel.admin.user.UserEventPageRequestVM;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface UserEventLogMapper extends MyMapper<UserEventLog> {

//    int deleteByPrimaryKey(Integer id);
//
//    int insert(UserEventLog record);
//
//    int insertSelective(UserEventLog record);
//
//    UserEventLog selectByPrimaryKey(Integer id);
//
//    int updateByPrimaryKeySelective(UserEventLog record);
//
//    int updateByPrimaryKey(UserEventLog record);

    String queryParams=" id, user_id userId, user_name userName, real_name realName, content, create_time createTime ";

    @Select("select " + queryParams +
            "from t_user_event_log\n" +
            "    where user_id=#{id}\n" +
            "    order by id desc\n" +
            "    limit 10")
    List<UserEventLog> getUserEventLogByUserId(@Param("id") Integer id);

    @Select("<script>" +
            "select " + queryParams +
            " from t_user_event_log " +
            "<if test=\"userId != null\">\n" +
            "         and user_id= #{userId}\n" +
            "      </if>\n" +
            "      <if test=\"userName != null\">\n" +
            "        and user_name= #{userName}\n" +
            "      </if>" +
            "</script>")
    List<UserEventLog> page(UserEventPageRequestVM requestVM);

    @Select("SELECT create_time as name,COUNT(create_time) as value from\n" +
            "\t\t        (\n" +
            "\t\t\t\t  SELECT DATE_FORMAT(create_time,'%Y-%m-%d') as create_time from t_user_event_log\n" +
            "\t\t\t\t\tWHERE  create_time  between  #{startTime}  and  #{endTime}\n" +
            "\t\t\t\t) a\n" +
            "\t\tGROUP BY create_time")
    List<KeyValue> selectCountByDate(@Param("startTime") Date startTime, @Param("endTime") Date endTime);
}
